﻿using Arms.Application.Modules.Security;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Arms.Presentation.Web.Controllers
{
    public abstract class BaseController : Controller
    {
        protected virtual new ArmsUser User
        {
            get { return base.User as ArmsUser; }
        }

        public virtual ArmsUserData UserData
        {
            get
            {
                var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                if (authCookie == null) return null;

                // extract the forms authentication cookie
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                if (authTicket == null) return null;

                if (string.IsNullOrWhiteSpace(authTicket.UserData)) return null;

                // extract user data from ticket
                return JsonConvert.DeserializeObject<ArmsUserData>(authTicket.UserData);
            }
        }

        protected RedirectToRouteResult RedirectToAction(string action, string controller, string area)
        {
            return RedirectToAction(action, controller, new { area = area });
        }
    }
}
